package com.iursp.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.iursp.conf.aop.LoggerManage;
import com.iursp.domain.model.role.AuthPlatform;
import com.iursp.domain.model.role.Role;
import com.iursp.domain.model.user.CommonUser;
import com.iursp.service.role.AuthRoleService;
import com.iursp.service.role.PlatformService;
import com.iursp.utils.UserUtils;

import io.swagger.annotations.ApiOperation;

/**
 * 
 * @ClassName:  HomeController   
 * @Description:跳转页面集成类----> 带参数，单纯的跳转页面在mvcConfig
 * @author: ruiwu
 * @date:   2018年6月21日 上午9:58:11   
 *
 */
@Controller
@RequestMapping("/")
public class HomeController extends BaseController{
	
	@Autowired
	private PlatformService platformService;
	
	@Autowired
	private AuthRoleService roleService;
	
	@ApiOperation(value="跳转到用户详情页", notes="页面跳转,跳转到用户详情页，携带status状态")
	@RequestMapping(value="/users/detail/{status}", method=RequestMethod.GET)
	public String toUserDetail(Model model,@PathVariable String status) {
		logger.info("跳转到用户详情页");
		model.addAttribute("status", status);	
		return "user/updateUser";
	}
	
	@ApiOperation(value="跳转到首页", notes="登录成功后,跳转到首页,携带username")
	@GetMapping(value = {"/index"})
    public ModelAndView index(@AuthenticationPrincipal CommonUser userDetails, Map<String, String> map) {
		UserUtils.setUser(userDetails);
        map.put("username", userDetails.getUsername());
        return new ModelAndView("index", map);
    }
	
	@ApiOperation(value="跳转到新增或者修改角色弹框", notes="跳转到新增或者修改角色弹框")
	@RequestMapping(value = "/auth/role/editRoleInfo", method = { RequestMethod.GET, RequestMethod.POST })
	public String editRoleInfo(Model model,
			@RequestParam(value="refid" ,required = false) String refid,
			@RequestParam(value="platformRefid" ,required = false) String platformRefid) {
		try {
			Role role = null;
			List<AuthPlatform> platformList = platformService.queryInnerPlatform();
			model.addAttribute("platformList", platformList);
			if (StringUtils.isNotBlank(refid)) {
				role = roleService.getRoleByRefid(refid);
			} else {
				CommonUser commonUser = UserUtils.getUser();
				role = new Role();
				role.setPlatformRefid(platformRefid);
				role.setActive(Boolean.TRUE);
				role.setDeleted(Boolean.FALSE);
				role.setCreateUserRefid(commonUser.getRefid());
				role.setModifyUserRefid(commonUser.getRefid());
				role.setVersion(1);
			}
			model.addAttribute("role", role);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("编辑角色信息发生异常,错误信息为:", e);
		}
		return "auth/update";
	}
	
	@ApiOperation(value="跳转到新增或者修改用户角色列表弹框", notes="跳转到新增或者修改用户角色列表弹框")
	@RequestMapping(value = "/auth/role/addUserRoleInfoView", method = { RequestMethod.GET, RequestMethod.POST })
	public String addUserRoleInfoView(Model model,
			@RequestParam(value="refid" ,required = false) String refid,
			@RequestParam(value="platformRefid" ,required = false) String platformRefid) {
		try {
			model.addAttribute("platformRefid", platformRefid);
			model.addAttribute("refid", refid);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("进入用户角色列表页面异常,信息为:",e);
		}
		return "user/addToRole";
	}
	
	@ApiOperation(value="统一跳转类", notes="通用公共跳转、根据{path}跳转")
	@RequestMapping(value = "/Jump/{path}", method = { RequestMethod.GET, RequestMethod.POST })
	public String Jump(Model model,@PathVariable String path,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("统一跳转类,路径"+path);
		try {
			if(StringUtils.isNotEmpty(path)) {
				String[] split = path.split("_");
				return split[0]+"/"+split[1];
			}else {
				return "error";
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("统一跳转异常,信息为:",e);
			return "error";
		}
	}
}